<template>
  <view>
    <view v-if="!idcard1" style="padding-top: 20upx;">
      <view class="webkit list mt bb">
        <view class="title">真实姓名</view>
        <input type="text" :value="realname" @input="realnameIpt" class="flex" />
      </view>
      <view class="webkit list mt bb">
        <view class="title">身份证号</view>
        <input type="text" :value="idcard" @input="idcardIpt" maxlength="18" class="flex" />
      </view>
      <view style="padding: 0 24upx;">
        <button type="primary" style="background: #EB2E15;margin-top: 20upx;" @click="submit">确认修改</button>
      </view>
    </view>
    <view v-if="idcard1" style="padding-top: 20upx;">
      <view class="webkit list mt bb">
        <view class="title">真实姓名</view>
        <input type="text" :value="realname" @input="realnameIpt" disabled="disabled" class="flex" />
      </view>
      <view class="webkit list mt bb">
        <view class="title">身份证号</view>
        <input type="text" :value="idcard" @input="idcardIpt" maxlength="18" disabled="disabled" class="flex" />
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        openid: '',
        realname: '',
        idcard: '',
        idcard1: '',
        pt: '',
      };
    },
    onLoad: function(e) {
      this.pt = e.pt
      uni.getStorage({
        key: 'openid',
        success: (res) => {
          this.openid = res.data
          this.setAjax()
        },
        fail: () => {
          uni.navigateTo({
            url: '../../login/login'
          });
        }
      });
    },
    methods: {
      setAjax() {
        uni.request({
          url: 'http://hszj.hongshantang696.com//app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=member.realname&app=1',
          method: 'POST',
          data: {
            'openid': this.openid
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            console.log(res)
            this.realname = res.data.realname
            this.idcard = res.data.idcard
            this.idcard1 = res.data.idcard
            uni.hideLoading()
          },
          fail: () => {},
          complete: () => {}
        });
      },
      realnameIpt(e) {
        // console.log(e.detail.value)
        this.realname = e.detail.value
      },
      idcardIpt(e) {
        this.idcard = e.detail.value
      },
      jump(e) {
        uni.navigateTo({
          url: e.currentTarget.dataset.src
        });
      },
      submit() {
        if (!(
            /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/
          )
          .test(this.idcard)) {
          return uni.showToast({
            title: '请输入正确的身份证号码',
            icon: "none"
          });
        }
        var o = this
        // console.log(this.sexnum)
        uni.showLoading()
        uni.request({
          url: 'http://hszj.hongshantang696.com//app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=member.realname_sub&app=1',
          method: 'POST',
          data: {
            'openid': this.openid,
            'realname': this.realname,
            'idcard': this.idcard,
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            uni.hideLoading()
            if (res.data.status == 1) {
              uni.showToast({
                title: '修改成功',
                icon: 'none'
              });
              setTimeout(() => { // 正常返回
                console.log(o.pt)
                if (o.pt == 1) {
                  uni.switchTab({
                    url: '/pages/shop/pt'
                  })
                } else {
                  uni.navigateBack();
                }
              }, 300)
            } else {
              uni.showToast({
                title: res.data.result.message,
                icon: "none"
              });
            }
          },
          fail: () => {},
          complete: () => {}
        });
      }
    }
  }
</script>
<style>
  .list {
    padding: 16upx 24upx;
    background: #fff;
  }

  .angle {
    position: relative;
    padding-right: 50upx;
  }

  .angle:after {
    content: " ";
    display: inline-block;
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
    height: 16upx;
    width: 16upx;
    border-width: 2upx 2upx 0 0;
    border-color: #b2b2b2;
    border-style: solid;
    position: absolute;
    top: 50%;
    right: 30upx;
    margin-top: -8upx;
    box-sizing: border-box;
  }

  .list .title {
    width: 130upx;
    line-height: 50upx;
    color: #666;
  }

  .list .flex {
    line-height: 50upx;
    color: #666;
    padding-left: 24upx;
  }

  .list .right {
    color: #999;
  }

  .cityp {
    text-align: left;
    font-size: 28upx;
    background: transparent;
  }

  .cityp:after {
    display: none;
  }

  .bb {
    position: relative;
  }

  .bb:before {
    content: " ";
    position: absolute;
    left: 20upx;
    right: 20upx;
    bottom: -2upx;
    height: 2upx;
    border-top: 1px solid #ebebeb;
    color: #D9D9D9;
    -webkit-transform-origin: 0 0;
    -ms-transform-origin: 0 0;
    transform-origin: 0 0;
    -webkit-transform: scaleY(0.5);
    -ms-transform: scaleY(0.5);
    transform: scaleY(0.5);
  }

  .layout {
    background: #7C2548;
    margin-top: 20upx;
  }
</style>
